package com.kykj.kykjyzw.controller;

import com.kykj.kykjyzw.entity.MenuViewEntity;
import com.kykj.kykjyzw.entity.ResultEntity;
import com.kykj.kykjyzw.entity.UserRoleViewEntity;
import com.kykj.kykjyzw.service.impl.AdminRoleServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v2/adminrole/")
@RestController
@CrossOrigin//解决跨域问题
public class AdminRoleController {
    @Resource
    AdminRoleServiceImpl adminRoleService;

    /**
     * 获取按钮列表
     * @param rolename 角色名
     * @param routename 路由名
     * @param limit 每行页数
     * @param page 页码
     * @return List
     */
    @GetMapping("getmenulist")
    private ResultEntity getMenuList(
        @RequestParam("rolename") String rolename,
        @RequestParam("routename") String routename,
        @RequestParam("limit") int limit,
        @RequestParam("page") int page
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            int start=(page-1)*limit;
            List<MenuViewEntity> list = adminRoleService.getMenuList(rolename,routename,start,limit);
            if (!routename.equals("")|| !rolename.equals("")){//有查询条件的情况下，获取当前条件下的所有行数
                resultEntity.setCount(adminRoleService.getMenuListCountByName(rolename,routename));
            }else { //没有查询条件的情况下,就获取所有行数
                resultEntity.setCount(adminRoleService.getMenuListCount());
            }
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setData(list);
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(9998);
            resultEntity.setMsg("数据获取失败！");
        }
        return resultEntity;
    }

    /**
     * 获取用户角色
     * @param rolename 角色名
     * @param uname 用户名
     * @param limit 每页行数
     * @param page 页码
     * @return ResultEntity
     */
    @GetMapping("getuserrolelist")
    private ResultEntity getUserRoleList(
            @RequestParam("rolename") String rolename,
            @RequestParam("uname") String uname,
            @RequestParam("limit") int limit,
            @RequestParam("page") int page
    ){
        ResultEntity resultEntity = new ResultEntity();
        try{
            int start = (page-1)*limit;
            List<UserRoleViewEntity> list = adminRoleService.getUserRoleList(rolename,uname,start,limit);
            if (!rolename.equals("")||!uname.equals("")){
                resultEntity.setCount(adminRoleService.getUserRoleListCountByName(rolename,uname));
            }else {
                resultEntity.setCount(adminRoleService.getUserRoleListCount());
            }
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setData(list);
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(9998);
            resultEntity.setMsg("数据获取失败！");
        }
        return  resultEntity;
    }
}
